<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasMesWorklineList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basLineDelete, basLineListData } from '/@/api/bas/mes/workline/basLine';
  import { mesLineListData , mesLineOpenLine , mesLineCloseLine } from '/@/api/mes/mo/line/mesOrderLinefp';
  import { basLineDisable, basLineEnable } from '/@/api/bas/mes/workline/basLine';
  import { basWorkShopTreeData } from '/@/api/bas/mes/workshop/basMesWorkShop';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { fa } from 'element-plus/es/locale';

  const { t } = useI18n('bas.mes.workline.basLine');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('产线管理'),
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('产线编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 100,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('产线名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('当前订单'),
      dataIndex: 'modid',
      key: 'a.modid',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('当前产品'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('上机时间'),
      dataIndex: 'upDate',
      key: 'a.up_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('最后下机时间'),
      dataIndex: 'downDate',
      key: 'a.down_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('产线状态'),
      dataIndex: 'lineStatus',
      key: 'a.line_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'mes_line_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        label: t('开机'),
        title: t('开机'),
        onClick: handleOpenLine.bind(this, { id: record.id }),
        auth: 'mo:basLine:open',
        ifShow: () => record.lineStatus === '9',
      },
      {
        label: t('关机'),
        title: t('关机'),
        popConfirm: {
          title: t('关机后，任务会自动下架，是否确认关机'),
          confirm: handleCloseLine.bind(this, { code: record.code }),
        },
        ifShow: () => record.lineStatus === '0' || record.lineStatus === '1',
        auth: 'mo:basLine:close',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: mesLineListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    showTableSetting: true,
    useSearchForm: false,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const res = await basLineDisable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleOpenLine(record: Recordable) {
    const res = await mesLineOpenLine(record);
    showMessage(res.message);
    handleSuccess();
  }
  // 关机
  async function handleCloseLine(record: Recordable) {
    const res = await mesLineCloseLine(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleEnable(record: Recordable) {
    const res = await basLineEnable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await basLineDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
